package com.baichuan.zntx.server;

import com.baichuan.zntx.conf.NettyProperties;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.nio.NioEventLoopGroup;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class NettyServerBoot {
    @Resource
    private NioEventLoopGroup boosGroup;

    @Resource
    private NioEventLoopGroup workerGroup;

    private final ServerBootstrap serverBootstrap;
    private final NettyProperties nettyProperties;

    @PostConstruct
    public void start() throws InterruptedException{
        serverBootstrap.bind(nettyProperties.getPort()).sync();
        serverBootstrap.bind(nettyProperties.getPortSlave()).sync();
        log.info("启动Netty: {},{}", nettyProperties.getPort(), nettyProperties.getPortSlave());
    }

    @PreDestroy
    public void close(){
        log.info("Server关闭");
        boosGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
